package jdbc;

import java.sql.*;
/*步骤：
 *     注册驱动--》建立连接--》获取链接--》定义SQL--》执行sql
 * */
public class jdbcDemo1 {
    public static void main(String[] args){
     Connection con=null;
     Statement st=null;
        try{
        //1导入jar包
        //2注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //3获取数据库链接对象
       con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sun","root","123456");
        //4定义sql语句
        String sql="select * from classes";
        //5获取执行sql对象
        st = con.createStatement();
        //6执行sql
            ResultSet tes = st.executeQuery(sql);
        //7处理结果
            while(tes.next()){
            int id=tes.getInt("id");
            String name=tes.getString("name");
            System.out.println("id="+id+" name="+name);}
        }catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //8释放资源
        finally {

                try { if(st!=null)
                    st.close();
                    if(con!=null)
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
        }


    }
}
